﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using NPOI.Util;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using WMSUIMVC.DTO.Unit;
using XuanKeZz.Application.HTTP;

namespace WMSUIMVC.Controllers
{
    public class UnitController : Controller
    {
        protected readonly IConfiguration _configuration;

        public UnitController(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 显示单位表
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowUnit()
        {
            return View();
        }

        /// <summary>
        /// 显示单位表
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowUnits()
        {
            return View();
        }
        /// <summary>
        /// 显示单位信息
        /// </summary>
        /// <param name="unit"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetUnitShow(SelectUnitDTO unit)
        {
            HttpClientWrapper client = new HttpClientWrapper(unit.Token);
            string url = _configuration["Url"]+ "sbd/api/UnitRead/SelectUnitModel?UnitName=" + unit.UnitName + "&UnitCode=" + unit.UnitCode + "&pageindex=" + unit.PageIndex + "&pagesize=" + unit.PageSize;
            string list = JsonConvert.SerializeObject(unit);
            string response = await client.PostAsync(url,list);
            return Json(response);
        }
        /// <summary>
        /// 添加单位信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> AddUnits(AddUnitDTO obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = _configuration["Url"] + "sbx/api/Unit/AddUnit";
            string datas = JsonConvert.SerializeObject(obj);
            var res = await client.PostAsync(url, datas);
            return Json(res);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="unit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetUnitFt(FanUnitDTO funit)
        {
            HttpClientWrapper client = new HttpClientWrapper(funit.Token);
            string url = _configuration["Url"] + "sbd/api/UnitRead/GetUnitById?UnitId=" + funit.UnitId;
            string GetUnit = await client.GetAsync(url);
            return Json(GetUnit);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<IActionResult> UpdUnits(UpdUnitDTO obj)
        {
            HttpClientWrapper client = new HttpClientWrapper(obj.Token);
            string url = _configuration["Url"] + "sbx/api/Unit/UpdUnit";
            string list = JsonConvert.SerializeObject(obj);
            string UpdUnit = await client.PutAsync(url, list);
            return Json(UpdUnit);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="unit"></param>
        /// <returns></returns>
        public async Task<IActionResult> DelUnits(DelUnitDTO unit)
        {
            HttpClientWrapper client = new HttpClientWrapper(unit.Token);
            string url = _configuration["Url"] + "sbx/api/Unit/DelUnit";
            string list = JsonConvert.SerializeObject(unit);
            string GetUnit = await client.PostAsync(url, list);
            return Json(GetUnit);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="unit"></param>
        /// <returns></returns>
        public async Task<IActionResult> DelAllUnits(DelUnitAllDTO unit)
        {
            HttpClientWrapper client = new HttpClientWrapper(unit.Token);
            string url = _configuration["Url"] + "sbx/api/Unit/DelAllUnit";
            string list = JsonConvert.SerializeObject(unit);
            //将集合转为json
            var res = await client.PostAsync(url, list);
            return Json(res);
        }
    }
}
